import { defineStore } from 'pinia';
import util from "@/common/tools/utils.js"
import { promise } from '../../uni_modules/uview-plus/libs/function/test';

/**
 * @typedef {Object} usePageStore
 * @property {Array} indexslides
 */
export const usePageStore = defineStore('pagestore', {
	state: () => {
		return { 
			pageBarName: 'home', //底部菜单的当前选中
			account :{}, //站点account数据
			w:{}, //对应微擎的全局变量W
			primayColor: '#FC9244',
			areas : [],
			inloading: false,
			siteinfoJs:{},//微擎生成的配置js
			wxapp:{},
			company:{},
			searchparams:{},
			navmenu: [],
			security:{},
			indexslides:[],
			showBindMobile:undefined,//显示绑定手机号弹窗
			isBindMobile: false,//是否绑定了手机号
			user:{}, 
			bgColor:{
				light: '#EDF5FF',
				less: '#C8E1FF',				
				second: '#4C9CF7',
				weight: '#4A9BF7'
			},
			shopCart :{}, //购物车数据
			backRefresh: undefined, //返回页面更新标识
		};
	},
	 getters:{
		webUrl: (state)=> {
			return state.siteinfoJs.siteroot.split("app/")[0] + "addons/kbwdl_nickscenery"
		}, 
		siteInfo:(state)=>{
			return ()=>{ return state.account; }
		},
		HostUri:(state)=> {
			var a = state.siteinfoJs;
			return a.siteroot + "?i="+a.uniacid;
			//return a.siteroot + "?i="+a.uniacid+"&t="+a.multiid+"&v="+a.version+"";
		} 
	},
	 
	actions: {
		setBackRefresh( tag ){
			this.backRefresh = tag;
		},
		 
		setConfig(value){
			this.siteinfoJs = value
		},
		setPageBarName( value ) {
			this.pageBarName = value;
		},
		setShopCart(value){
			this.shopCart = value;
		},
		//从深页面重新返回到首页的某个tab里用在从深层页面返回；比如购物下单后返回
		switchTab(name){
			this.pageBarName = name;
			let fun = function(){
				uni.reLaunch({
					url: "/pages/index"
				})
			}
			try{
				setTimeout(fun, 200)
			}catch(e){
				console.log(e)
			}
			
		},
		//获取小程序的配置参数
		initdata(){
			return  util.getModuleData("webfront/option").then( ret =>{
				//幻灯片
			 this.user = ret.user; //当前用户
			 //this.w = ret.w;
			 this.account = ret.account;
			 this.indexslides = ret.indexslides ??[];
			 //this.wxapp = ret.wxapp;
			 this.company = ret.company;			 
				//首页导航菜单
			 this.navmenu = ret.navmenu;
			 //this.searchparams = ret.display;
			 //发消息
			 uni.$emit('initdataCompleted', ret);
			 //this.areas = ret.areas_quan;
			 //this.security = ret.security;
				return new   Promise(resolve=> resolve(ret))
			 
			}); 
		},
		getWxappAccount(){
			//防止重复请求数据
			if(!this?.account?.name && !this.inloading){
				
				this.inloading = true;
				return util.getSiteInfo().then( ret =>  {
					this.settWxappAccount (ret?.account);
					this.inloading = false;
					return new Promise( resolve=> resolve(ret?.account ))
				});
			}else {
				return new Promise( resolve=> resolve(this.account ))
			}
			
		},
		settWxappAccount( value ){ 
			this.account = value; 
		},
		
		//用于获取broker数据，并且已经赋值到store.broker	
		getBroker(){
			return  util.getModuleData("webfront/isbroker").then(ret =>{
				 if(ret) {	 
					 this.broker = ret;
				 }
				 return new Promise(resolve=>{
					 resolve(ret)
				 })
			}) ;
			 
		},
		//暂时不用，调用这个方法可以直接弹出绑定的modal
		isBroker(){			 
		  return new Promise((resolve, reject)=>{
			  util.getModuleData("webfront/isbroker").then(ret =>{
			  	  			 
			  	 if(!ret) {
			  		 //没有绑定显示绑定窗口
			  		 this.showBindMobile = !this.showBindMobile;
			  	 }else{
			  		 this.isBindMobile = true;
			  		 this.broker = ret;
			  	 }
			  	   resolve(ret);
			  }) 
		  })			
				 
		},
		
		jumpto(url, target){
			 
			if(target == 2){
				wx.navigateToMiniProgram({
					 shortLink: url,
					 //develop开发版；trial体验版；release正式版
					 envVersion: 'release', 
				})
			}else if(target ==1 ){
				wx.navigateTo({
					url :"/pages/webview/webview?url="+ url,
				})
			}else{
				wx.navigateTo({url: url})
			}
			
			
		},
		
		callPhone(mobile){
			if(mobile && mobile.length>10) 
			 uni.makePhoneCall({
				phoneNumber: mobile
			 })
			 else
				uni.showToast({
					icon:"error",
					title: '暂无联系信息'
				})
		},
		
	},
});